from sqlalchemy.orm import relationship, backref

from database_connection.index import Base, engine
from sqlalchemy import Text, Column, Integer, Boolean, String


class Admin(Base):  # 必须继承declaraive_base得到的那个基类
    __tablename__ = "Admin"  # 必须要有__tablename__来指出这个类对应什么表，这个表可以暂时在库中不存在，SQLAlchemy会帮我们创建这个表
    id = Column(Integer, nullable=False, primary_key=True, autoincrement=True, unique=True, comment="id")
    user_name = Column(String(16, collation='utf8_bin'), nullable=False)  # 用户名
    user_pwd = Column(String(16, collation='utf8_bin'), nullable=False)  # 密码
    user_img = Column(Text, nullable=False)  # 用户头像
    is_administrators = Column(Boolean, nullable=False, default=False)  # 是否是管理员
    cookie_id = Column(Text, nullable=False)  # 用户的cookie
    mail = Column(String(255, collation='utf8_bin'), nullable=True)  # 用户的邮箱号
    like = Column(Text(length=4294967295), nullable=False, default="")

    message = relationship('Message', back_populates='admin')  # 用户的评论
    admin_blog_list = relationship("Blog", secondary="Admin_Link_Blog", back_populates="blog_admin_list")

    def admin_to_dict(self):
        return {
            'id': self.id,
            'user_name': self.user_name,
            'user_pwd': self.user_pwd,
            'user_img': self.user_img,
            'is_administrators': self.is_administrators,
            'cookie_id': self.cookie_id,
            'mail': self.mail,
        }

    def __init__(self, user_name, user_pwd, user_img, is_administrators, cookie_id, mail):
        self.user_name = user_name
        self.user_pwd = user_pwd
        self.user_img = user_img
        self.is_administrators = is_administrators
        self.cookie_id = cookie_id
        self.mail = mail



Base.metadata.create_all(engine)
